import 'dart:async';
import 'dart:convert';
import 'dart:core';
import 'package:dio/dio.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_app_05/data/model.dart';
import 'package:flutter_app_05/http/nets.dart';
import 'package:flutter_app_05/widget/stateful_widget_extends.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:shared_preferences/shared_preferences.dart';


/// login in
class UserLocalInfo extends Object {
  String phone;
  String password;
  String data;

  UserLocalInfo({this.phone, this.password, this.data});
}

/// integralList
class IntegralList extends Object {
  String token;
  String min;
  String max;

  IntegralList({this.token, this.min, this.max});
}

class DataManager extends Object {
  static Future<bool> saveUserLocalInfo(
      {@required UserLocalInfo userLocalInfo}) async {
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      prefs.setString('phone', userLocalInfo.phone);
      prefs.setString('password', userLocalInfo.password);
      prefs.setString('token', userLocalInfo.data);
      return true;
    } catch (e) {
      // debugPrint('---> DataManager.saveUserLocalInfo e:$e');
      return false;
    }
  }


///***************************************业务逻辑*****************************************************

  static Future<PengZhangJson> getPzxs(
      {String token, Map<String,String> p,TStateMixin context}) async {
    PengZhangJson easeModel=null;
  var res = await Nets.dio
      .post(RequestPaths.urlBase + RequestPaths.balance_extend,
    data: jsonEncode(p),).then((value) {
    easeModel = PengZhangJson.fromJson(value.data);
  });


    //     .then((value) => {
    //       print(value.toString()+"get valuse"),
    // });



    // print("mmmmmmmmmmmmmmmmmmm"+easeModel.toString());

    return easeModel;
  }
  static Future<ZuLiFJson> getBmz(
      {String token, Map<String,String> p,TStateMixin context}) async {
    ZuLiFJson easeModel=null;
    var res = await Nets.dio
        .post(RequestPaths.urlBase + RequestPaths.balance_comDn,
      data: jsonEncode(p),).then((value) {
      easeModel = ZuLiFJson.fromJson(value.data);
    });


    //     .then((value) => {
    //       print(value.toString()+"get valuse"),
    // });



    // print("mmmmmmmmmmmmmmmmmmm"+easeModel.toString());

    return easeModel;
  }
  static Future<ZuliSJson> getZl(
      {String token, Map<String,String> p,TStateMixin context}) async {
    ZuliSJson easeModel=null;
    var res = await Nets.dio
        .post(RequestPaths.urlBase + RequestPaths.balance_comPa,
      data: jsonEncode(p),).then((value) {
      easeModel = ZuliSJson.fromJson(value.data);
    });


    //     .then((value) => {
    //       print(value.toString()+"get valuse"),
    // });



    // print("mmmmmmmmmmmmmmmmmmm"+easeModel.toString());

    return easeModel;
  }
  static Future<ShuiBengJson> getSbxx(
      {String token, Map<String,String> p,TStateMixin context}) async {
    ShuiBengJson easeModel=null;
    var res = await Nets.dio
        .post(RequestPaths.urlBase + RequestPaths.balance_Pump,
      data: jsonEncode(p),).then((value) {
      easeModel = ShuiBengJson.fromJson(value.data);
    });


    //     .then((value) => {
    //       print(value.toString()+"get valuse"),
    // });



    // print("mmmmmmmmmmmmmmmmmmm"+easeModel.toString());

    return easeModel;
  }
  static Future<HrqJson> getRsqjs(
      {String token, Map<String,String> p,TStateMixin context}) async {
    HrqJson easeModel=null;
    var res = await Nets.dio
        .post(RequestPaths.urlBase + RequestPaths.balance_comHeat,
      data: jsonEncode(p),).then((value) {
      easeModel = HrqJson.fromJson(value.data);
    });


    //     .then((value) => {
    //       print(value.toString()+"get valuse"),
    // });



    // print("mmmmmmmmmmmmmmmmmmm"+easeModel.toString());

    return easeModel;
  }

  static Future<GuanJingJson> getGj(
      {String token, Map<String,String> p,TStateMixin context}) async {
    GuanJingJson easeModel=null;
    var res = await Nets.dio
        .post(RequestPaths.urlBase + RequestPaths.balance_comDnsel,
      data: jsonEncode(p),).then((value) {
      easeModel = GuanJingJson.fromJson(value.data);
      print(value.toString());
    });
    return easeModel;
  }

  static Future<FormLcJson> getGetlcb(
      {String token, Map<String,String> p,TStateMixin context}) async {
    FormLcJson easeModel=null;
    var res = await Nets.dio
        .post(RequestPaths.urlBase + RequestPaths.forum_getlcb,
      data: jsonEncode(p),).then((value) {
      easeModel = FormLcJson.fromJson(value.data);
      print(value.toString());
    });
    return easeModel;
  }

  static Future<FormLcJson> getOther(
      {String token, Map<String,String> p,TStateMixin context}) async {
    FormLcJson easeModel=null;
    var res = await Nets.dio
        .post(RequestPaths.urlBase + RequestPaths.forum_getother,
      data: jsonEncode(p),).then((value) {
      easeModel = FormLcJson.fromJson(value.data);
      print(value.toString());
    });
    return easeModel;
  }
  static Future<FormLcJson> requestLogin(
      {String token, Map<String,String> p,TStateMixin context}) async {
    FormLcJson easeModel=null;
    var res = await Nets.dio
        .post(RequestPaths.urlBase + RequestPaths.URL_LOGIN,
      data: jsonEncode(p),).then((value) {
      easeModel = FormLcJson.fromJson(value.data);
      print(value.toString());
    });
    return easeModel;
  }


  static Future<LoginJson> login(
      {String token, Map<String,String> p,TStateMixin context}) async {
    LoginJson easeModel=null;
    var res = await Nets.dio
        .post(RequestPaths.urlBase + RequestPaths.URL_LOGIN,
      data: jsonEncode(p),).then((value) {
      easeModel = LoginJson.fromJson(value.data);
      print(value.toString());
    });
    return easeModel;
  }

  static pageToIndex() {
    DataEvent event = DataEvent(DataEventType.ChangePageToIndex, '');
    eventBus.fire(event);
  }

  static pageLoginOrRegister() {
    DataEvent event = DataEvent(DataEventType.ChangePageToLoginOrRegister, '');
    eventBus.fire(event);
  }
}

EventBus eventBus = EventBus();
enum DataEventType {
  UserChanged,
  RefreshNews,
  RefreshMine,
  ChangePageToIndex,
  ChangePageToLoginOrRegister,
}

class DataEvent {
  DataEventType eventType;
  dynamic data;

  DataEvent(this.eventType, this.data);
}

class UserChangeNotifier extends ChangeNotifier {
  // UserInfoJson _user;
  //
  // UserInfoJson get currentUser => _user;
  //
  // set user(UserInfoJson u) {
  //   _user = u;
  //   notifyListeners();
  // }
}
